<?php
  /**
  Author: shielamarie
  Date: Oct 2011
  Contains all methods available to an admin user.
  **/
	class AdminController
	{
		private $d;

		const PRE_ELEMENTARY = 1;
		const ELEMENTARY = 2;
		//
		public function __construct()
		{
			$this->d = new DbHelper();
		}
		public function create($student)
		{
			$i = $student['id'];
			$n = $student['name'];
			$c = $student['section'];
			$p = $student['payment'];
			$s = "INSERT INTO Students VALUES ('$i', '$n', '$c', '$p')";
			return $this->d->query($s);
		}
		public function readElementaryDetails($studentId)
		{
		  $query = "SELECT * FROM ElementaryDetails WHERE StudentId = $studentId";
		  return $this->d->query($query);
		}
		public function readPreElementaryDetails($studentId)
		{
		  $query = "SELECT * FROM PreElementaryDetails WHERE StudentId = $studentId";
		  return $this->d->query($query);
		}
		public function update($student)
		{
			$i = $student['id'];
			$n = $student['name'];
			$c = $student['section'];
			$b = $student['balance'];
			$p = $student['payment'];
			$remainingBalance = (int)$b - (int)$p;
			$s = "UPDATE Students SET Id='$i', Name='$n', Section='$c', " .
					"Balance='$remainingBalance' WHERE Id = $i";
			$this->d->query($s);
		}
		public function updatePassword($password)
		{
		  session_start();
		  $u = $_SESSION['loggedUser'];
		  $uid = $u['UserId'];
		  //
		  $query = "UPDATE Users SET Password= '$password' WHERE UserId='$uid'";
		  $this->d->query($query);
		}
		public function delete($studentId)
		{
			$s = "DELETE FROM Students WHERE Id = $studentId";
			$this->d->query($s);
		}
		public function getStudentCount($departmentId) {
		  $query = "SELECT COUNT(`Id`) FROM Students
		            WHERE DepartmentId = '$departmentId'";
		  return $this->d->query($query);
		}
		public function getDepartmentList()
		{
		  $query = "SELECT * FROM Departments";
		  return $this->d->query($query);
		}
		public function addYearLevel($yearLevelName,$departmentId)
		{
		  $query = "INSERT INTO YearLevels(YearLevelDescription,DepartmentId)
		            VALUES('$yearLevelName','$departmentId')";
		  return $this->d->query($query);
		}
		public function getYearLevel()
		{
		  $query = "SELECT * FROM YearLevels";
		  return $this->d->query($query);
		}
		public function getYearLevelName($id)
		{
		  $query = "SELECT * FROM YearLevels WHERE YearLevelId = '$id'";
		  $details = mysql_fetch_assoc($this->d->query($query));

		  return $details['YearLevelDescription'];
		}
		public function getYearLevelByDepartment($departmentId)
		{
		  $query = "SELECT * FROM YearLevels
		            WHERE DepartmentId = $departmentId";
		  return $this->d->query($query);
		}
		public function addSubject($subjectName,$yearLevel)
		{
      $query = "INSERT INTO Subjects(SubjectName,YearLevelId)
                VALUES('$subjectName','$yearLevel')";
      return $this->d->query($query);
		}
    public function getSubjectList()
    {
      $query = "SELECT Subjects.Id, Subjects.YearLevelId,YearLevels.YearLevelDescription, Subjects.Name
                FROM YearLevels
                INNER JOIN Subjects
                ON YearLevels.YearLevelId = Subjects.YearLevelId
                ORDER BY Subjects.YearLevelId DESC";
      return $this->d->query($query);
    }
    public function getSubjectListByYear($yearLevelId)
    {
       $query = "SELECT SubjectId,SubjectName
                  FROM Subjects
                  WHERE YearLevelId = '$yearLevelId'";
      return  $this->d->query($query);
    }
    public function removeSubject($subjectId)
    {
      $query = "DELETE FROM Subjects WHERE SubjectId = '$subjectId'";
      $this->d->query($query);
    }
    public function addTeacher($teachersName, $yearLevel)
    {
      $query = "INSERT INTO Teachers(TeachersName,YearLevelId)
                VALUES('$teachersName','$yearLevel')";
      return $this->d->query($query);
    }
    public function getTeacherList()
    {
      $query = "SELECT Teachers.TeacherId, Teachers.TeachersName, YearLevels.YearLevelDescription
                FROM Teachers
                INNER JOIN YearLevels
                ON Teachers.YearLevelId = YearLevels.YearLevelId
                ORDER BY Teachers.TeachersName ASC";
      return $this->d->query($query);
    }
    public function getTeacherDetails($tid)
     {
      $query = "SELECT * FROM Teachers
                WHERE TeacherId = $tid";
      return $this->d->query($query);
    }
    public function updateTeacherDetails($tid,$teachersName,$yid)
    {
      $query = "UPDATE Teachers SET TeachersName='$teachersName', YearLevelId = '$yid'
                WHERE TeacherId= '$tid'";
      $this->d->query($query);
    }
    public function removeTeacher($id)
    {
      $query = "DELETE FROM Teachers WHERE TeacherId = '$id'";
      $this->d->query($query);
    }
    public function getFeesList()
    {
      $query = "SELECT Fees.FeeId, Fees.YearLevelId,Fees.FeeName, Fees.FeeAmount, YearLevels.YearLevelDescription
                FROM Fees
                INNER JOIN YearLevels
                ON Fees.YearLevelId = YearLevels.YearLevelId";
      return $this->d->query($query);
    }
    public function addFees($feeName,$feeAmount,$yearId)
    {
      $query = "INSERT INTO Fees(FeeName,FeeAmount,YearLevelId)
                VALUES('$feeName','$feeAmount','$yearId')";
      return $this->d->query($query);
    }
    public function getFees($fid)
    {
      $query = "SELECT * FROM Fees
                WHERE Fees.FeeId = '$fid'";
      return $this->d->query($query);
    }
    public function getFeesByDepartment($id) {
      $query = "SELECT FeeName,FeeAmount
                FROM Fees
                WHERE DepartmentId = '$id'";
      return $this->d->query($query);
    }
    public function getFeesByYearLevel($id)
    {
      $query = "SELECT FeeName, FeeAmount
                FROM Fees
                WHERE YearLevelId = '$id'";
      return $this->d->query($query);
    }
    public function addStudentFees($id)
    {
      $m = $_POST;
      $total= $m['total'];
      //
      $query = "UPDATE Students SET TotalFees = '$total' WHERE StudentId = '$id'";
      return $this->d->query($query);
    }
    public function updateFees($id,$feeName,$feeAmount,$yearId)
    {
      $query = "UPDATE Fees SET FeeName='$feeName', FeeAmount = '$feeAmount',YearLevelId= '$yearId'
                WHERE FeeId = '$id'";
      return $this->d->query($query);
    }
    public function removeFees($id) {
      $query = "DELETE FROM Fees
                WHERE Fees.FeeId = '$id'";
      return $this->d->query($query);
    }
    public function addPaymentLog($studentId, $amount, $receiptNumber,$paymentDate)
    {
      $s = "(SELECT StudentId FROM Students WHERE StudentId = '$studentId')";
      $query = "INSERT INTO PaymentLogs(StudentId,PaymentDate,PaymentAmount,ReceiptNumber)
                VALUES($s,'$paymentDate','$amount','$receiptNumber')";
     $this->d->query($query);
     echo $query;
    }
    public function removePaymentLogs($id)
    {
      $query = "DELETE FROM PaymentLogs
              WHERE PaymentLogs.PaymentId = '$id'";
      $this->d->query($query);
    }
    public function getPaymentLogs()
    {
      $query = "SELECT * FROM PaymentLogs";
      return $this->d->query($query);
    }
    public function getPaymentLogsById($id)
    {
      $query = "SELECT * FROM PaymentLogs WHERE PaymentId='$id'";
      return $this->d->query($query);
    }
    public function getPaymentLogsByStudent($id)
    {
      $query = "SELECT * FROM PaymentLogs WHERE StudentId = '$id'";
      return $this->d->query($query);
    }
    public function updatePaymentLogs($id)
    {
      $m = $_POST;
      $sid = $m['student-id'];
      $date = $m['payment-date'];
      $receipt = $m['receipt-number'];
      $payment = $m['payment-amount'];
      //
      $query = "UPDATE PaymentLogs SET StudentId = '$sid',
                PaymentDate = '$date',
                PaymentAmount = '$payment',
                ReceiptNumber = '$receipt'
                WHERE PaymentId = '$id'";
     return $this->d->query($query);
    }
    public function getStudentDetails($id)
    {
      $query = "SELECT * FROM Students
                WHERE StudentId='$id'";
      return $this->d->query($query);
    }
	}
?>